package com.whlilong.jxc.dialog;

import java.text.DecimalFormat;
import java.text.NumberFormat;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TrayDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.swtdesigner.SWTResourceManager;


public class PayDialog extends TrayDialog {
	private String sumMoney ;
	private Label info;
	private Text textPay;
	private Text text;
	private String payMoney;
	private double pay;
	private double sum;
	private double change;
	private boolean isSuccess = false;

	public PayDialog(Shell parentShell, String sumMoney) {
		// TODO Auto-generated constructor stub
		super(parentShell);
		this.sumMoney = sumMoney;
	}
	
//	protected int getShellStyle() {
//		return super.getShellStyle() | SWT.RESIZE;
//	}
	
	protected Control createDialogArea(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		GridLayout gl_composite = new GridLayout();
		gl_composite.verticalSpacing = 10;
		gl_composite.marginBottom = 10;
		gl_composite.numColumns = 2;
		composite.setLayout(gl_composite);
		composite.setLayoutData(new GridData(GridData.FILL_BOTH));
		info = new Label(composite, SWT.None);
		info.setForeground(SWTResourceManager.getColor(255, 0, 0));
		Label labelSumMoney = new Label(composite, SWT.NONE);
		labelSumMoney.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		labelSumMoney.setFont(SWTResourceManager.getFont("楷体", 20, SWT.BOLD));
		labelSumMoney.setText("合计金额：");
		
		text = new Text(composite, SWT.BORDER);
		text.setText(sumMoney);
		text.setEnabled(false);
		text.setFont(SWTResourceManager.getFont("楷体", 20, SWT.BOLD));
		text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label labelPay = new Label(composite, SWT.NONE);
		labelPay.setFont(SWTResourceManager.getFont("楷体", 20, SWT.BOLD));
		labelPay.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		labelPay.setText("实收金额：");
		
		textPay = new Text(composite, SWT.BORDER);
		textPay.setFont(SWTResourceManager.getFont("楷体", 20, SWT.BOLD));
		textPay.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Font font = new Font(Display.getCurrent(), "宋体", 15, SWT.NONE);
		
		GridData data = new GridData(GridData.FILL_HORIZONTAL);
		data.horizontalIndent = 10;
		data.horizontalSpan = 2;
		info.setLayoutData(data);
		info.setFont(SWTResourceManager.getFont("楷体", 20, SWT.BOLD));
		info.setText(""); //$NON-NLS-1$
		
//		int width = getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT).x;
//		if (width < 460 || width == 490)
//			width = 500;
//		getShell().setSize(width, 600);
		
		addListener();
		
		return composite;
	}

	private void addListener() {
		// TODO 监听付款文本框的操作
		textPay.addModifyListener(new ModifyListener(){

			@Override
			public void modifyText(ModifyEvent e) {
				// TODO 判断付款金额输入格式的合法性
				isValidate();
				
			}
			
		});
		
	}

	protected void isValidate() {
		// TODO 判断付款金额输入格式的合法性
		String regex = "[0-9]+.{0,1}[0-9]*";
		payMoney = textPay.getText();
		if(!payMoney.matches(regex) && !payMoney.equals("")){
			info.setText("付款金额格式有误！");
			isSuccess = false;
			getButton(OK).setEnabled(isSuccess);
		}
		else{
			if(payMoney.equals("")||payMoney==null){
				info.setText("");
				isSuccess = false;
				getButton(OK).setEnabled(isSuccess);
				return;
			}
			pay = Double.parseDouble(payMoney);
			sum = Double.parseDouble(sumMoney);
			if( sum > pay){
				info.setText("付款不足！");
				isSuccess = false;
				getButton(OK).setEnabled(isSuccess);
			}else{
				NumberFormat f = new DecimalFormat("#0.00");
				change = pay-sum;
				info.setText("请找零："+ f.format(change));
				isSuccess = true;
				getButton(OK).setEnabled(isSuccess);
			}
		}
		
	}

	protected Point getInitialSize() {
		Point pt = new Point(400, 200);
		makeShellAlignInCenter(getShell());
		return pt;
	}

	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("商品付款");
	}
	
	protected void createButtonsForButtonBar(Composite parent) {
		// create OK and Cancel buttons by default
		createButton(parent, IDialogConstants.OK_ID, "付款(&S)", true);
		createButton(parent, IDialogConstants.CANCEL_ID, "取消(&X)", false);
		getButton(OK).setEnabled(isSuccess);

	}
	
	
	/**
	 * Position the shell in the center of screen
	 * 
	 * @param shell
	 */
	protected void makeShellAlignInCenter(Shell shell) {
		Rectangle screenSize = Display.getDefault().getClientArea();
		Rectangle shellBounds = shell.getBounds();
		int x = (screenSize.width - shellBounds.width) >> 1;
		int y = (screenSize.height - shellBounds.height) >> 1;
		shell.setLocation(x, y);
	}

	public double getPay() {
		return pay;
	}

	public void setPay(double pay) {
		this.pay = pay;
	}

	public double getChange() {
		return change;
	}

	public void setChange(double change) {
		this.change = change;
	}

	public boolean isSuccess() {
		return isSuccess;
	}

	public void setSuccess(boolean isSuccess) {
		this.isSuccess = isSuccess;
	}
	
}
